import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_general/pages/takePic/takePicPageModel.dart';
import 'package:image_picker/image_picker.dart';
import 'package:pkg_obz/export.dart';
import 'package:pkg_obz/widgets/button_widgets.dart';
import 'package:pkg_obz/widgets/common_widgets.dart';
import 'package:pkg_obz/widgets/dialog.dart';
import 'package:pkg_obz/widgets/text_widgets.dart';
import 'package:pkg_pic_show/pkg_pic_show.dart';
import 'package:video_player/video_player.dart';

/// Create： Zhang 2021/6/28 1:40 下午
/// erp:     ext.zhangjunfei
/// 描述：照片选择
class TakePicPage extends PageStateful<TakePicPageModel> {
  TakePicPage() : super(title: "照片选择", pageModel: TakePicPageModel());

  ///图片选择
  final ImagePicker _picker = ImagePicker();

  @override
  Widget? content(BuildContext context) {
    return SingleChildScrollView(
      padding: EdgeInsets.all(20),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          commonText("选择照片", fontSize: 16, fontWeight: FontWeight.w700),
          Row(
            children: [
              pageModel.selectPicUri == null
                  ? cardView(
                      isShowShadow: false,
                      width: 150,
                      height: 150,
                      margin: EdgeInsets.only(top: 15, bottom: 15),
                    )
                  : clickAble(
                      child: ClipRRect(
                        borderRadius: BorderRadius.circular(4),
                        child: Image.file(
                          File(pageModel.selectPicUri!),
                          width: 150,
                          height: 150,
                          fit: BoxFit.cover,
                        ),
                      ),
                      onClick: () => ZRouter.goTo(
                        ShowPic(
                          imageUrls: [pageModel.selectPicUri!],
                          index: 0,
                          isDark: true,
                        ),
                      ),
                    ),
              clickAble(
                child: cardView(
                  isShowShadow: false,
                  width: 150,
                  height: 150,
                  margin: EdgeInsets.only(top: 15, bottom: 15, left: 15),
                  child: Icon(
                    Icons.add,
                    size: 40,
                  ),
                ),
                onClick: () {
                  showBottomMenu(
                    items: ["相机", "图库"],
                    callback: (index, item) {
                      switch (item) {
                        case "相机":
                          _takePic(ImageSource.camera);
                          break;
                        case "图库":
                          _takePic(ImageSource.gallery);
                          break;
                      }
                    },
                  );
                },
              )
            ],
          ),
          commonText("选择视频", fontSize: 16, fontWeight: FontWeight.w700),
          pageModel.videoController == null
              ? clickAble(
                  child: cardView(
                    isShowShadow: false,
                    width: double.infinity,
                    height: 200,
                    margin: EdgeInsets.only(top: 15, bottom: 15),
                    child: Icon(
                      Icons.add,
                      size: 40,
                    ),
                  ),
                  onClick: () {
                    showBottomMenu(
                      items: ["相机", "图库"],
                      callback: (index, item) {
                        switch (item) {
                          case "相机":
                            _takeVideo(ImageSource.camera);
                            break;
                          case "图库":
                            _takeVideo(ImageSource.gallery);
                            break;
                        }
                      },
                    );
                  },
                )
              : Padding(
                  padding: EdgeInsets.only(top: 15, bottom: 15),
                  child: ClipRRect(
                    borderRadius: BorderRadius.circular(10),
                    child: AspectRatio(
                      aspectRatio: pageModel.videoController!.value.aspectRatio,
                      child: VideoPlayer(pageModel.videoController!),
                    ),
                  ),
                ),
        ],
      ),
    );
  }

//--------------方法定义-------------
  ///获取照片
  void _takePic(ImageSource imageSource) async {
    final pickedFile = await _picker.pickImage(
      source: imageSource,
      maxHeight: 1280,
      maxWidth: 720,
    );
    pageModel.setPicUri(pickedFile!.path);
  }

  ///获取视频
  void _takeVideo(ImageSource imageSource) async {
    final pickedFile = await _picker.pickVideo(
      source: imageSource,
      // maxDuration: const Duration(seconds: 10),
    );

    pageModel.videoController =
        VideoPlayerController.file(File(pickedFile!.path))
          ..initialize()
          ..setVolume(0.5)
          ..setLooping(true)
          ..play();

    setState();
  }
//--------------方法定义-------------
}
